package com.pjedu.springbootmybatismapper02.service.serviceimp;

import com.pjedu.springbootmybatismapper02.entity.User;
import com.pjedu.springbootmybatismapper02.mapper.UserMapper;
import com.pjedu.springbootmybatismapper02.service.UserService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 汪洋
 * @Description
 */
@Service("userService")
public class UserServiceImp implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public List<User> findAll(){
        return userMapper.selectAll();
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> findAllByConditions(User user) {
        return userMapper.findAllByConditions(user);
    }

    @Override
    public List<User> findUsersLikeUsernames(String username) {
        //创建一个Example对象
        Example example = new Example(User.class);
        //判断你是否需要减少查询的字段
        example.selectProperties("username","password");
        //构建一个规则对象
        Example.Criteria criteria = example.createCriteria();
        //书写指定的规则内容
        criteria.andLike("username","%" + username + "%");
        //调用通用mapper里面的查询方法,使用example对象来进行查询
        return userMapper.selectByExample(example);
    }
}
